package com.ujoku.controller.admin;

import com.labillusion.core.platform.web.pagination.PaginationForm;
import com.labillusion.core.platform.web.pagination.PaginationView;
import com.labillusion.core.platform.web.rest.RESTController;
import com.labillusion.core.util.StringUtils;
import com.ujoku.context.constants.ContextConstants;
import com.ujoku.context.constants.MessageConstants;
import com.ujoku.domain.*;
import com.ujoku.domain.admin.Parameter;
import com.ujoku.request.admin.OrderListForm;
import com.ujoku.service.MemberService;
import com.ujoku.service.MessageService;
import com.ujoku.service.MyListItemService;
import com.ujoku.service.OrderService;
import com.ujoku.service.admin.ParametersService;
import com.ujoku.view.domain.admin.MyListMemberItemView;
import com.ujoku.view.domain.admin.MyListMemberView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by suggestion on 2015/2/1.
 */
@RestController
public class OrderMngController extends RESTController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private ParametersService parametersService;

    @Autowired
    private MyListItemService myListItemService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private MessageService messageService;

    @RequestMapping(value = "/uj-admin/order/list", method = RequestMethod.POST)
    @ResponseBody
    public PaginationView list(@RequestBody @Valid OrderListForm form) {
        Map<String, Object> query = form.getQueryMap();

        if (StringUtils.hasText(form.getId()))
            query.put("id", form.getId());
        if (StringUtils.hasText(form.getStatus()))
            query.put("status", form.getStatus());

        PaginationView<Order> view = new PaginationView<>();

        List<Order> list = orderService.selectList(query);

        view.setList(list);
        int count = orderService.selectCount(query);
        view.setCount(count);

        return view;

    }

    @RequestMapping(value = "/uj-admin/order/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Order getById(@PathVariable String id) {

        Order order = (Order)orderService.selectById(id);

        Parameter p = parametersService.getParameterByKey(ContextConstants.MEMBER_LEVEL + order.getLevel());
        if (p != null) {
            order.setLevelName(p.getMark());
            order.setLevelDiscount(Double.parseDouble(p.getValue()));
        }

        return order;
    }

    @RequestMapping(value = "/uj-admin/order/mylist/{id}", method = RequestMethod.GET)
    @ResponseBody
    public MyListMemberView getMyListMember(@PathVariable String id) {
        Order order = (Order)orderService.selectById(id);

        MyListMemberView view = new MyListMemberView();
        for(OrderItem orderItem : order.getOrderItemList()){
            if(!StringUtils.hasText(orderItem.getMyListItemId())){
                continue;
            }

            MyListItem myListItem =  (MyListItem) myListItemService.selectById(orderItem.getMyListItemId());
            if(myListItem == null)
                continue;

            MyListMemberItemView itemView = new MyListMemberItemView();
            itemView.setGoodsName(orderItem.getGoodsName());
            itemView.setMemberName(myListItem.getMemberName());
            itemView.setQty(orderItem.getQty());
            itemView.setPrice(orderItem.getPrice());
            itemView.setTotal(orderItem.getTotal());
            view.getList().add(itemView);
        }

        if(view.getList().size() > 0)
            return view;

        return null;
    }

    @RequestMapping(value = "/uj-admin/shipOrder/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public Order shipped(@PathVariable String id){
        Order order = (Order)orderService.selectById(id);

        order.setStatus(OrderStatus.Shipped);

        orderService.update(order);

        Parameter p = parametersService.getParameterByKey(ContextConstants.MEMBER_LEVEL + order.getLevel());
        if (p != null) {
            order.setLevelName(p.getMark());
            order.setLevelDiscount(Double.parseDouble(p.getValue()));
        }

        //私人酒窖充值
        setCellarMasterBalance(order);

        //通知消息
        Message message = messageService.bulid(MessageConstants.SHIPPED,order.getMemberId(), new String[]{order.getId()});
        messageService.insert(message);

        return order;
    }

    @RequestMapping(value = "/uj-admin/shippingInfo", method = RequestMethod.PUT)
    @ResponseBody
    public Order shippingInfo(@RequestBody @Valid Order order){
        orderService.update(order);

        Parameter p = parametersService.getParameterByKey(ContextConstants.MEMBER_LEVEL + order.getLevel());
        if (p != null) {
            order.setLevelName(p.getMark());
            order.setLevelDiscount(Double.parseDouble(p.getValue()));
        }

        return order;
    }

    public void setCellarMasterBalance(Order order){
        for(OrderItem orderItem : order.getOrderItemList()) {
            if (!StringUtils.hasText(orderItem.getMyListItemId())) {
                continue;
            }

            MyListItem myListItem =  (MyListItem) myListItemService.selectById(orderItem.getMyListItemId());
            if(myListItem == null)
                continue;

            Member member = (Member) memberService.selectById(myListItem.getMemberId());
            if(member == null)
                continue;

            member.setBalance(member.getBalance() + orderItem.getTotal());
            memberService.update(member);
        }
    }

}
